package RentCarProject;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RegisterUI extends JFrame {
    JFrame frame;
    JTextField usertext;
    JPasswordField passtext1,passtext2;
    JLabel userlabel,passwordlabel1,passwordlabel2,bglabel;
    MyButton returnloginbutton,registerbuttton;
    public RegisterUI(){
        frame=this;
        setTitle("用户注册");
        setSize(750,450);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        usertext=new JTextField(20);
        usertext.setBounds(330,100,180,20);
        passtext1=new JPasswordField(20);
        passtext1.setBounds(330,150,180,20);
        passtext2=new JPasswordField(20);
        passtext2.setBounds(330,200,180,20);
        userlabel=new JLabel("用户名：");
        userlabel.setFont(new Font("宋体",Font.BOLD,15));
        userlabel.setForeground(Color.white);
        userlabel.setBounds(250,100,80,20);
        passwordlabel1=new JLabel("密码：");
        passwordlabel1.setFont(new Font("宋体",Font.BOLD,15));
        passwordlabel1.setForeground(Color.white);
        passwordlabel1.setBounds(250,150,80,20);
        passwordlabel2=new JLabel("确认密码：");
        passwordlabel2.setFont(new Font("宋体",Font.BOLD,15));
        passwordlabel2.setForeground(Color.white);
        passwordlabel2.setBounds(250,200,80,20);
        bglabel=new JLabel(new ImageIcon("src/picture/register.png"));
        bglabel.setOpaque(false);
        bglabel.setBounds(0,0,750,450);

        returnloginbutton=new MyButton("返回登录");
        returnloginbutton.setBounds(250,270,100,40);
        registerbuttton=new MyButton("注册账号");
        registerbuttton.setBounds(400,270,100,40);

        returnloginbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new LoginUI();
                dispose();
            }
        });
        registerbuttton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UserBase userBase=new UserBase();
                userBase.setUsername(usertext.getText());
                if(passtext1.getText().equals(passtext2.getText()))
                    userBase.setPassword(passtext1.getText());
                else JOptionPane.showMessageDialog(frame,"注册失败！（两次输入密码不一致）");
                try {
                    if(usertext.getText().isEmpty()||passtext1.getText().isEmpty()){
                        JOptionPane.showMessageDialog(frame,"用户名或密码不能为空！");
                    }else{
                        if(userBase.isreGister()){
                            JOptionPane.showMessageDialog(frame,"账号已注册！");
                        }else{
                            if(userBase.reGister()){
                                JOptionPane.showMessageDialog(frame,"注册成功！");
                            }else{
                                JOptionPane.showMessageDialog(frame,"注册失败！");
                            }
                        }
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        add(userlabel);
        add(usertext);
        add(passwordlabel1);
        add(passtext1);
        add(passwordlabel2);
        add(passtext2);
        add(returnloginbutton);
        add(registerbuttton);
        add(bglabel);
        setVisible(true);
    }

    public static void main(String[] args) {
        new RegisterUI();
    }
}
